// pages/mine/order/list.js
const App = getApp()
import Kits from '../../../utils/kits'

Component({
  options: {
    addGlobalClass: true,
  },
  pageLifetimes:{
    show() {  // 对应页面的 onShow
      this.execLoad(true);  // 调用现有的数据加载方法
      console.log("shoWWW")
    },
   
  },
  properties: {
    state: {
      type: Number,
      default: 0
    }
  },
  data: {
    init: false,
    loading: false,
    triggered: true,
    CustomBar: App.Global.CustomBar,
    page: 1,
    size: 8,
    time: new Date().getTime(),
    pager: {
      now: 1,
      row: 0,
      all: 0,
      fms: 0,
    },
    items: []
  },

  lifetimes: {
    attached() {
      wx.sub('ReloadUserOrderList', this, () => {
        this.execLoad(false)
      })
      wx.sub('ReloadPayState', this, () => {
        this.execLoad(false)
      })
      this.execLoad(false)
    },
    detached() {
      wx.cancel('ReloadUserOrderList', this)
      wx.cancel('ReloadPayState', this)
    },
  },
  methods: {
    doPulldown() {
      this.data.page = 1
      this.execLoad(true)
    },
    doReachBottom() {
      if (this.data.page >= this.data.pager.all) {
        return
      }
      this.data.page = this.data.page + 1
      this.execLoad(true)
    },

    execLoad(pulldown) {
      this.setData({
        loading: true
      })
      App.doPost({
        uri: '/app/order/info/list',
        body: {
          pager: {
            page: this.data.page,
            size: this.data.size,
            time: this.data.time
          },
          query: {
            state: parseInt(this.data.state)
          }
        },
        fnSucc: resp => {
          if (resp.data.pager.now == 1) {
            this.data.time = resp.data.pager.fms
          }
          resp.data.items.forEach(item => {
            item.amountStr = Kits.fmtMoneyString(item.amount)
            item.hdays = Kits.getHourMemo(item.hours)
          })

          this.setData({
            init: true,
            loading: false,
            triggered: false,
            pager: resp.data.pager,
            items: resp.data.pager.now == 1 ? resp.data.items : this.data.items.concat(resp.data.items)
          })
        },
        fnComp: () => {
          if (pulldown) {
            this.triggerEvent('stopPullDown', {})
          }
        }
      })
    },
    // 去下单
    toCreate(e) {
      let now = Kits.getNextTime(new Date().getTime())
      let order = e.currentTarget.dataset.order
      wx.redirectTo({
        url: `/pages/order/create?carid=${order.car.id}&begin=${now.getTime()}&close=${now.addDays(2).getTime()}&onsite=&onsite_area=&onsite_locs=&onsite_addr=`,
      })
    },
    // 去支付
    toPayment(e) {
      console.log(e.currentTarget.dataset.order)
      let order = e.currentTarget.dataset.order
      let biz = e.currentTarget.dataset.biz
      let total = 0
      if (biz == 'Rent') {
        total = order.rent.real
      } else if (biz == 'Deposit') {
        total = order.deposit.total
      } else if (biz == 'Supp') {
        total = order.rent.real - order.rent.paid - order.rent.other_plat_paid
      }else if (biz == 'Extend') {
        total = order.rent.real - order.rent.paid - order.rent.other_plat_paid
      }
      // if (total <= 0) {
      //   return 
      // }
      console.log(e.currentTarget.dataset,"shushusuhus")
      let info = {
        id: order.id,
        expire: 900,
        total: total
      }
      wx.navigateTo({
        url: `/pages/order/pay?src=list&biz=${biz}&info=${ JSON.stringify(info) }`,
      })
    },
    // 去详情
    toDetail(e) {
      // console.log(e.currentTarget.dataset.order,"wqreret")
      let order = e.currentTarget.dataset.order
      wx.navigateTo({
        // /pages/mine/order/detail?oid=${order.id}
        url: `/pages/mine/order/detail?oid=${order.id}`,
      })
    }
  }
})